﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Net.Mail;

namespace BugSnapper
{
    public partial class frmGlobalSettings : Form
    {
        private delegate void GenericWindowsEventsHandler<TArgs>(object sender, TArgs e);

        private Logger Logger { get; set; }

        public frmGlobalSettings()
        {
            InitializeComponent();
            this.Logger = new Logger();
            try
            {
                this.SetDefaultDateValues();
            }
            catch (Exception ex)
            {
                this.txtDirectoryPath.Text = string.Empty;
                this.txtLineWidth.Text = "5";
                this.Logger.ProcessError(ex);
            }
        }
        private delegate void GenericEventsHandler();
        private void SetDefaultDateValues()
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(AppGlobal.RootFolderPath))
                        {
                            this.txtDirectoryPath.Text = AppGlobal.RootFolderPath;
                        }
                        else
                        {
                            this.txtDirectoryPath.Text = Convert.ToString(System.Environment.CurrentDirectory) + @"\tmpImages\";
                        }
                        if (!string.IsNullOrEmpty(AppGlobal.LineWidth))
                        {
                            this.txtLineWidth.Text = AppGlobal.LineWidth;
                        }
                        else
                        {
                            this.txtLineWidth.Text = "5";
                        }
                        if (AppGlobal.TimerPeriod > 0)
                        {
                            this.chkPeriodicSnaps.Checked = true;
                            this.txtTimerPeriod.Text = AppGlobal.TimerPeriod.ToString();
                        }
                        else
                        {
                            this.chkPeriodicSnaps.Checked = false;
                            this.txtTimerPeriod.Text = "0";
                        }
                        this.chkKeyLess.Checked = AppGlobal.KeyLess;
                        this.chkFitToPage.Checked = AppGlobal.FitToPage;
                        this.chkCompress.Checked = AppGlobal.Compress;
                        this.txtWaterMark.Text = AppGlobal.WaterMarkText;
                        this.chkTimeStampAsWaterMark.Checked = AppGlobal.DateTimeAsWaterMark;
                        switch (AppGlobal.SaveDocumentAs)
                        {
                            case AppGlobal.SaveDocumentFormat.Word:
                                {
                                    rdWord.Checked = true;
                                    break;
                                }
                            case AppGlobal.SaveDocumentFormat.PDF:
                                {
                                    rdPDF.Checked = true;
                                    break;
                                }
                            default:
                                {
                                    rdWord.Checked = true;
                                    break;
                                }
                        }
                        switch (AppGlobal.SaveAsImageFormat)
                        {
                            case AppGlobal.ImageFormats.BMP:
                                {
                                    this.cmbImageFormats.SelectedItem = "BMP";
                                    break;
                                }
                            case AppGlobal.ImageFormats.JPEG:
                                {
                                    this.cmbImageFormats.SelectedItem = "JPEG";
                                    break;
                                }
                            case AppGlobal.ImageFormats.GIF:
                                {
                                    this.cmbImageFormats.SelectedItem = "GIF";
                                    break;
                                }
                            case AppGlobal.ImageFormats.TIFF:
                                {
                                    this.cmbImageFormats.SelectedItem = "TIFF";
                                    break;
                                }
                            case AppGlobal.ImageFormats.PNG:
                                {
                                    this.cmbImageFormats.SelectedItem = "PNG";
                                    break;
                                }
                            default:
                                {
                                    this.cmbImageFormats.SelectedItem = "BMP";
                                    break;
                                }
                        }
                        this.txtDirectoryPathBG.Text = AppGlobal.VideoRootFolderPath;
                        this.txtVideoRating.Text = AppGlobal.VideoRating;
                        this.txtVideoDescription.Text = AppGlobal.VideoDescription;
                        this.txtVideoCopyRight.Text = AppGlobal.VideoCopyright;
                        this.txtVideoTitle.Text = AppGlobal.VideoTitle;
                        this.txtVideoAuthor.Text = AppGlobal.VideoAuthor;
                        this.txtAuthorEmail.Text = AppGlobal.VideoAuthorEmail;
                        this.txtAuthorWebsite.Text = AppGlobal.VideoAuthorWebsiteURL;
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericEventsHandler(SetDefaultDateValues));
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        using (FolderBrowserDialog folderBrowseDialog = new FolderBrowserDialog())
                        {
                            folderBrowseDialog.ShowDialog();
                            if (!string.IsNullOrEmpty(folderBrowseDialog.SelectedPath))
                            {
                                this.txtDirectoryPath.Text = folderBrowseDialog.SelectedPath;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(btnBrowse_Click), sender, e);
                }
            }
        }

        private static void AddRule(string filePath, string account, FileSystemRights rights, AccessControlType controlType)
        {
            FileSecurity fSecurity = File.GetAccessControl(filePath);
            fSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType));
            File.SetAccessControl(filePath, fSecurity);
        }

        private static void SetRule(string filePath, string account, FileSystemRights rights, AccessControlType controlType)
        {
            FileSecurity fSecurity = File.GetAccessControl(filePath);
            fSecurity.ResetAccessRule(new FileSystemAccessRule(account, rights, controlType));
            File.SetAccessControl(filePath, fSecurity);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
            }
            else
            {
                Invoke(new GenericWindowsEventsHandler<EventArgs>(btnClose_Click), sender, e);
            }
        }

        private void txtLineWidth_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8)
                        {
                            e.Handled = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<KeyPressEventArgs>(txtLineWidth_KeyPress), sender, e);
                }
            }
        }

        private void bntSave_Click(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(txtLineWidth.Text))
                        {
                            AppGlobal.LineWidth = txtLineWidth.Text;
                        }
                        if (chkPeriodicSnaps.Checked)
                        {
                            int tempValue = 0;
                            if (!string.IsNullOrEmpty(txtTimerPeriod.Text) && int.TryParse(txtTimerPeriod.Text, out tempValue))
                            {
                                if (tempValue > 0)
                                {
                                    AppGlobal.TimerPeriod = tempValue;
                                }
                                else
                                {
                                    AppGlobal.TimerPeriod = 0;
                                }
                            }
                        }
                        else
                        {
                            AppGlobal.TimerPeriod = 0;
                        }
                        AppGlobal.Compress = this.chkCompress.Checked;
                        AppGlobal.FitToPage = this.chkFitToPage.Checked;
                        if (rdPDF.Checked)
                        {
                            AppGlobal.SaveDocumentAs = AppGlobal.SaveDocumentFormat.PDF;
                        }
                        if (rdWord.Checked)
                        {
                            AppGlobal.SaveDocumentAs = AppGlobal.SaveDocumentFormat.Word;
                        }
                        switch (Convert.ToString(this.cmbImageFormats.SelectedItem))
                        {
                            case "BMP":
                                {
                                    AppGlobal.SaveAsImageFormat = AppGlobal.ImageFormats.BMP;
                                    break;
                                }
                            case "JPEG":
                                {
                                    AppGlobal.SaveAsImageFormat = AppGlobal.ImageFormats.JPEG;
                                    break;
                                }
                            case "GIF":
                                {
                                    AppGlobal.SaveAsImageFormat = AppGlobal.ImageFormats.GIF;
                                    break;
                                }
                            case "TIFF":
                                {
                                    AppGlobal.SaveAsImageFormat = AppGlobal.ImageFormats.TIFF;
                                    break;
                                }
                            case "PNG":
                                {
                                    AppGlobal.SaveAsImageFormat = AppGlobal.ImageFormats.PNG;
                                    break;
                                }
                            default:
                                {
                                    AppGlobal.SaveAsImageFormat = AppGlobal.ImageFormats.BMP;
                                    break;
                                }
                        }
                        if (!string.IsNullOrEmpty(txtDirectoryPath.Text))
                        {
                            AppGlobal.RootFolderPath = txtDirectoryPath.Text;
                        }
                        else
                        {
                            AppGlobal.RootFolderPath = string.Empty;
                        }
                        if (!string.IsNullOrEmpty(this.txtWaterMark.Text))
                        {
                            AppGlobal.WaterMarkText = this.txtWaterMark.Text;
                        }
                        else
                        {
                            AppGlobal.WaterMarkText = string.Empty;
                        }
                        AppGlobal.DateTimeAsWaterMark = this.chkTimeStampAsWaterMark.Checked;
                        AppGlobal.KeyLess = this.chkKeyLess.Checked;
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(bntSave_Click), sender, e);
                }
            }
        }

        private void txtTimerPeriod_Leave(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        if (this.chkPeriodicSnaps.Checked && string.IsNullOrEmpty(this.txtTimerPeriod.Text))
                        {
                            this.txtTimerPeriod.Text = "0";
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(txtTimerPeriod_Leave), sender, e);
                }
            }
        }

        private void chkPeriodicSnaps_CheckedChanged(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        if (this.chkPeriodicSnaps.Checked)
                        {
                            txtTimerPeriod.Visible = true;
                            lblTimerPeriod.Visible = true;
                            this.txtTimerPeriod.Text = AppGlobal.TimerPeriod.ToString();
                        }
                        else
                        {
                            this.txtTimerPeriod.Text = "0";
                            txtTimerPeriod.Visible = false;
                            lblTimerPeriod.Visible = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(chkPeriodicSnaps_CheckedChanged), sender, e);
                }
            }
        }

        private void btnBrowse_MouseHover(object sender, EventArgs e)
        {
            try
            {
                this.toolTip1.SetToolTip(btnBrowse, "Browse");
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        private void bntSave_MouseHover(object sender, EventArgs e)
        {
            try
            {
                this.toolTip1.SetToolTip(bntSave, "Save");
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        private void btnClose_MouseHover(object sender, EventArgs e)
        {
            try
            {
                this.toolTip1.SetToolTip(btnClose, "Cancel");
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }
        public bool IsEmailValid(string emailaddress)
        {
            try
            {
                MailAddress m = new MailAddress(emailaddress);
                return true;
            }
            catch (FormatException ex)
            {
                this.Logger.ProcessError(ex);
                return false;
            }
        }

        private void btnSaveBG_Click(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(this.txtAuthorEmail.Text) && !this.IsEmailValid(this.txtAuthorEmail.Text))
                        {
                            this.txtSearchSelectionErrorProvider.SetError(txtAuthorEmail, "Invalid Email");
                            return;
                        }
                        else
                        {
                            this.txtSearchSelectionErrorProvider.SetError(txtAuthorEmail, "");
                            AppGlobal.VideoRootFolderPath = this.txtDirectoryPathBG.Text;
                            AppGlobal.VideoRating = this.txtVideoRating.Text;
                            AppGlobal.VideoDescription = this.txtVideoDescription.Text;
                            AppGlobal.VideoCopyright = this.txtVideoCopyRight.Text;
                            AppGlobal.VideoTitle = this.txtVideoTitle.Text;
                            AppGlobal.VideoAuthor = this.txtVideoAuthor.Text;
                            AppGlobal.VideoAuthorEmail = this.txtAuthorEmail.Text;
                            AppGlobal.VideoAuthorWebsiteURL = this.txtAuthorWebsite.Text;
                            this.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(btnBrowse_Click), sender, e);
                }
            }
        }

        private void btnBrowseBG_Click(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        using (FolderBrowserDialog folderBrowseDialog = new FolderBrowserDialog())
                        {
                            folderBrowseDialog.ShowDialog();
                            if (!string.IsNullOrEmpty(folderBrowseDialog.SelectedPath))
                            {
                                this.txtDirectoryPathBG.Text = folderBrowseDialog.SelectedPath;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(btnBrowse_Click), sender, e);
                }
            }
        }

        private void rdWord_CheckedChanged(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        if (rdWord.Checked)
                        {
                            lblPDFCheckRecommendation.Visible = false;
                        }
                        if (rdPDF.Checked)
                        {
                            lblPDFCheckRecommendation.Visible = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(rdWord_CheckedChanged), sender, e);
                }
            }
        }
    }
}
